<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form id="form">
        <input type="text" name="name" id="name">
        <input type="text" age="age" id="age">
        <input type="text" sex="sex" id="sex">
        <button id="btn">按钮</button>
    <script>
        const oName = document.querySelector('#name')
        const oAge = document.querySelector('#age')
        const oSex = document.querySelector('#sex')
        const oform = document.querySelector('#form')
        const oBtn = document.querySelector('#btn')
        
        

        oBtn.onclick =function(){

        const nameValue = oName.value
        const ageValue = oAge.value
        const sexValue = oSex.value

        const xhr = new XMLHttpRequest()
        xhr.open('post','/user')
        // xhr.setRequestHeader('Content-Type','application/JSON')

        // xhr.send(JSON.stringify({
        //     name:nameValue,
        //     age:ageValue,
        //     sex:sexValue
        // })
        // )


        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.send(`name=${nameValue}&age=${ageValue}&sex=${sexValue}`)
        
        xhr.onreadystatechange =function(){
            if(xhr.readyState === 4){
                console.log(JSON.parse(xhr.responseText));
            }
        }
        return false
        }
        
    </script>
        
    </form>
</body>
</html>